package com.blrs.controller;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.blrs.common.core.page.PageDomain;
import com.blrs.common.core.page.TableSupport;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.page.PaginationUtil;
import com.blrs.domain.QuestionnaireReception;
import com.blrs.service.questionnaire.IQuestionnaireInvestigationService;
import com.blrs.service.questionnaire.IQuestionnaireReceptionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.domain.QuestionnaireInvestigation;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;

/**
 * 问卷调查Controller
 * 
 * @author yang
 * @date 2023-11-27
 */
@RestController
@RequestMapping("/questionnaire/investigation")
public class QuestionnaireInvestigationController extends BaseController
{
    @Autowired
    private IQuestionnaireInvestigationService questionnaireInvestigationService;
    @Autowired
    private IQuestionnaireReceptionService questionnaireReceptionService;

    /**
     * 查询问卷调查列表
     */
    @GetMapping("/list")
    public TableDataInfo list(QuestionnaireInvestigation questionnaireInvestigation)
    {
        startPage();
        List<QuestionnaireInvestigation> list = questionnaireInvestigationService.selectQuestionnaireInvestigationList(questionnaireInvestigation);
        return getDataTable(list);
    }

    @GetMapping("/listOrgId")
    public TableDataInfo listOrgId(QuestionnaireInvestigation questionnaireInvestigation)
    {
        startPage();
        List<QuestionnaireInvestigation> list = questionnaireInvestigationService.selectQuestionnaireInvestigationListByOrgId(questionnaireInvestigation);
        return getDataTable(list);
    }

    /**
     * 根据客户id查询问卷调查列表
     */
        @GetMapping("/listByEnterpriseId")
    public TableDataInfo listByEnterpriseId(QuestionnaireInvestigation questionnaireInvestigation)
    {
        startPage();
        List<QuestionnaireInvestigation> list= Collections.emptyList();
         if (Objects.nonNull(questionnaireInvestigation.getReceiverId())){
             list= questionnaireInvestigationService.selectQuestionnaireInvestigationListReception(questionnaireInvestigation);
         }
        return getDataTable(list);
    }
//    @GetMapping("/listByEnterpriseId")
//    public TableDataInfo listByEnterpriseId(QuestionnaireInvestigation questionnaireInvestigation)
//    {
//        startPage();
//        List<QuestionnaireInvestigation> list = questionnaireInvestigationService.selectQuestionnaireInvestigationByEnterpriseIdList(questionnaireInvestigation);
//        return getDataTable(list);
//    }
    /**
     * 导出问卷调查列表
     */
    @Log(title = "问卷调查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuestionnaireInvestigation questionnaireInvestigation)
    {
        List<QuestionnaireInvestigation> list = questionnaireInvestigationService.selectQuestionnaireInvestigationList(questionnaireInvestigation);
        ExcelUtil<QuestionnaireInvestigation> util = new ExcelUtil<QuestionnaireInvestigation>(QuestionnaireInvestigation.class);
        util.exportExcel(response, list, "问卷调查数据");
    }

    /**
     * 获取问卷调查详细信息
     */
    @GetMapping(value = "/{questionnaireId}")
    public AjaxResult getInfo(@PathVariable("questionnaireId") String questionnaireId)
    {
        return success(questionnaireInvestigationService.selectQuestionnaireInvestigationByQuestionnaireId(questionnaireId));
    }

    /**
     * 新增问卷调查
     */
    @Log(title = "问卷调查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionnaireInvestigation questionnaireInvestigation) throws NoSuchFieldException, IllegalAccessException {
        return success(questionnaireInvestigationService.insertQuestionnaireInvestigation(questionnaireInvestigation));
    }

    /**
     * 修改问卷调查
     */
    @Log(title = "问卷调查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionnaireInvestigation questionnaireInvestigation)
    {
        return toAjax(questionnaireInvestigationService.updateQuestionnaireInvestigation(questionnaireInvestigation));
    }

    /**
     * 删除问卷调查
     */
    @Log(title = "问卷调查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{questionnaireIds}")
    public AjaxResult remove(@PathVariable String[] questionnaireIds)
    {
        return toAjax(questionnaireInvestigationService.deleteQuestionnaireInvestigationByQuestionnaireIds(questionnaireIds));
    }

    /**
     * 查询历史问卷
     */
    @GetMapping("/hisList")
    public TableDataInfo hislist(QuestionnaireInvestigation questionnaireInvestigation)
    {
        startPage();
        List<QuestionnaireInvestigation> list = questionnaireInvestigationService.selectQuestionnaireInvestigationListHis(questionnaireInvestigation);
        return getDataTable(list);
    }

    /**
     * 查询待审核的问卷列表
     */
    @GetMapping("/listByExamine")
    public TableDataInfo listByExamine(QuestionnaireInvestigation questionnaireInvestigation)
    {
        startPage();
//        questionnaireInvestigation.setStatus("审核");
        //questionnaireInvestigation.setExamineId(SecurityUtils.getUserId());
        List<QuestionnaireInvestigation> list = questionnaireInvestigationService.selectQuestionnaireInvestigationListByExamine(questionnaireInvestigation);
        return getDataTable(list);
    }
    /**
     * 审核问卷调查
     */
    @PostMapping("/examine")
    public AjaxResult examine(@RequestBody QuestionnaireInvestigation questionnaireInvestigation){
       return questionnaireInvestigationService.examineById(questionnaireInvestigation);
    }

    /**
     * 查询发送企业列表
     */
    @GetMapping ("/sendList")
    public TableDataInfo sendList(QuestionnaireReception questionnaireReception){
        {
            //startPage();
            PageDomain pageDomain = TableSupport.buildPageRequest();
            Integer pageNum = pageDomain.getPageNum();
            Integer pageSize = pageDomain.getPageSize();
            List<QuestionnaireReception> list = questionnaireReceptionService.selectQuestionnaireReceptionList(questionnaireReception);
            return PaginationUtil.getPageInfo(list, pageNum, pageSize);
        }
    }

    /**
     * 查询当前用户的问卷接收列表
     */
    @GetMapping("/receptionList")
    public TableDataInfo receptionList(QuestionnaireInvestigation questionnaireInvestigation){
        startPage();
        questionnaireInvestigation.setReceiverId(String.valueOf(SecurityUtils.getOrgId()));
        List<QuestionnaireInvestigation> list=questionnaireInvestigationService.selectQuestionnaireInvestigationListReception(questionnaireInvestigation);
        return getDataTable(list);
    }

    /**
     * 提交审核
     */
    @GetMapping("/toExamine/{id}")
    public AjaxResult toExamine(@PathVariable(name = "id")String id){
        return  questionnaireInvestigationService.selectQuestionnaireInvestigationExamineById(id);
    }
}
